package com.mmall.controller.backend;

import com.mmall.common.Const;
import com.mmall.common.ResponseCode;
import com.mmall.common.ServerResponse;
import com.mmall.pojo.Category;
import com.mmall.pojo.User;
import com.mmall.service.ICategoryService;
import com.mmall.service.IUserService;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * Create by lisy on Qingdao
 **/
@RequestMapping("/manage/category")
@Controller
public class CategoryManageController {

    @Autowired
    IUserService iUserService;
    @Autowired
    ICategoryService iCategoryService;

    /**
     * ####2.增加节点
     * /manage/category/add_category.do
     */
    @RequestMapping(value = "add_category.do", method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse addCategory(HttpSession session, String categoryName, @RequestParam(value = "parentId", defaultValue = "0") int parentId){
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        if(user == null){
            //用户未登录
            return ServerResponse.createByErrorMssage(ResponseCode.NEED_LOGIN.getCode(), "addCategory需要登录啊");
        }
        //用户登录了，要判断用户是否为管理员
        if(iUserService.checkUserRole(user).isSuccess()){
            //处理增加分类的逻辑
            return iCategoryService.addCategoryService(categoryName, parentId);
        }else {
            return ServerResponse.createByError();
        }

    }

    /**
     * ####3.修改品类名字
     * http://localhost:8080/manage/category/set_category_name.do?categoryId=999&categoryName=%E5%98%BB%E5%98%BB
     * http://localhost:8080/manage/category/set_category_name.do?categoryId=1&categoryName=%E5%98%BB%E5%98%BB
     * /manage/category/set_category_name.do
     */
    @RequestMapping(value = "set_category_name.do", method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse setCategoryName(HttpSession session, int categoryId, String categoryName){
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        if(user == null){
            //用户未登录
            return ServerResponse.createByErrorMssage(ResponseCode.NEED_LOGIN.getCode(), "addCategory需要登录啊");
        }
        //用户登录了，要判断用户是否为管理员
        if(iUserService.checkUserRole(user).isSuccess()){
            //调用service方法修改名字
            return iCategoryService.setCategoryNameService(categoryId, categoryName);
        }else {
            return ServerResponse.createByErrorMssage("无权限修改");
        }
    }

    /**
     * ####1.获取品类子节点(平级)
     * http://localhost:8080/manage/category/get_category.do
     * http://localhost:8080/manage/category/get_category.do?categoryId=0
     * http://localhost:8080/manage/category/get_category.do?categoryId=2
     * /manage/category/get_category.do
     */
    @RequestMapping(value = "get_category.do", method = RequestMethod.GET)
    @ResponseBody
    public ServerResponse getChildrenParallenCategory(HttpSession session, @RequestParam(value = "categoryId", defaultValue = "0") int categoryId){
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        if(user == null){
            //用户未登录
            return ServerResponse.createByErrorMssage(ResponseCode.NEED_LOGIN.getCode(), "addCategory需要登录啊");
        }
        //用户登录了，要判断用户是否为管理员
        if(iUserService.checkUserRole(user).isSuccess()){
            //取品类子节点
             return iCategoryService.getChildrenParallelCategoryService(categoryId);
        }else {
            return ServerResponse.createByErrorMssage("无权限修改");
        }
    }

    /**
     * ####4.获取当前分类id及递归子节点categoryI
     * http://localhost:8080/manage/category/get_deep_category.do?categoryId=100001
     * /manage/category/get_deep_category.do
     */
    @RequestMapping(value = "get_deep_category.do", method = RequestMethod.GET)
    @ResponseBody
    public ServerResponse getDeepCategory(HttpSession session, @RequestParam(value = "categoryId", defaultValue = "0") int categoryId){
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        if(user == null){
            //用户未登录
            return ServerResponse.createByErrorMssage(ResponseCode.NEED_LOGIN.getCode(), "addCategory需要登录啊");
        }
        //用户登录了，要判断用户是否为管理员
        if(iUserService.checkUserRole(user).isSuccess()){
            //调用递归方法
            return iCategoryService.selectCategoryAndChildrenById(categoryId);
        }else {
            return ServerResponse.createByErrorMssage("无权限修改");
        }
    }

}
